import Mock from 'mockjs'
import { success, fail, handlePagination, formatDate, generateId } from './utils'

// 生成协议数据
const agreements = Array.from({ length: 50 }, (_, index) => ({
  id: generateId(),
  name: Mock.Random.word(5, 10),
  type: Mock.Random.pick(['通信协议', '安全协议', '数据协议']),
  description: Mock.Random.sentence(10, 20),
  status: Mock.Random.pick(['enabled', 'disabled']),
  agreementnameinfo: {
    version: '1.0.0',
    protocolType: Mock.Random.pick(['TCP', 'UDP', 'HTTP']),
    commands: Array.from({ length: Mock.Random.integer(3, 8) }, () => ({
      code: Mock.Random.integer(1000, 9999),
      name: Mock.Random.word(4, 8),
      description: Mock.Random.sentence(5, 10)
    })),
    errorCodes: Array.from({ length: Mock.Random.integer(2, 5) }, () => ({
      code: Mock.Random.integer(4000, 5000),
      message: Mock.Random.sentence(3, 8)
    }))
  },
  creator: Mock.Random.name(),
  createTime: formatDate(new Date(Mock.Random.datetime())),
  updateTime: formatDate(new Date(Mock.Random.datetime()))
}))

// 获取协议列表
export function setupAgreementMock(mock) {
  mock.onGet('/api/agreement/list').reply(config => {
    const { keyword, type, status, page = 1, pageSize = 10 } = config.params || {}
    
    let filteredData = [...agreements]
    
    if (keyword) {
      filteredData = filteredData.filter(item => 
        item.name.toLowerCase().includes(keyword.toLowerCase()) ||
        item.description.toLowerCase().includes(keyword.toLowerCase())
      )
    }
    
    if (type) {
      filteredData = filteredData.filter(item => item.type === type)
    }
    
    if (status) {
      filteredData = filteredData.filter(item => item.status === status)
    }
    
    return [200, success(handlePagination(filteredData, page, pageSize))]
  })

  // 获取协议详情
  mock.onGet(new RegExp('/api/agreement/detail/\\d+')).reply(config => {
    const id = config.url.split('/').pop()
    const agreement = agreements.find(item => item.id === id)
    return agreement ? [200, success(agreement)] : [404, fail('协议不存在')]
  })

  // 创建协议
  mock.onPost('/api/agreement/create').reply(config => {
    const newAgreement = {
      ...JSON.parse(config.data),
      id: generateId(),
      createTime: formatDate(new Date()),
      updateTime: formatDate(new Date())
    }
    agreements.unshift(newAgreement)
    return [200, success(newAgreement)]
  })

  // 更新协议
  mock.onPut(new RegExp('/api/agreement/update/\\d+')).reply(config => {
    const id = config.url.split('/').pop()
    const index = agreements.findIndex(item => item.id === id)
    if (index === -1) return [404, fail('协议不存在')]
    
    const updatedAgreement = {
      ...agreements[index],
      ...JSON.parse(config.data),
      updateTime: formatDate(new Date())
    }
    agreements[index] = updatedAgreement
    return [200, success(updatedAgreement)]
  })

  // 更新协议状态
  mock.onPut(new RegExp('/api/agreement/status/\\d+')).reply(config => {
    const id = config.url.split('/').pop()
    const index = agreements.findIndex(item => item.id === id)
    if (index === -1) return [404, fail('协议不存在')]
    
    const newStatus = JSON.parse(config.data).status
    agreements[index].status = newStatus
    agreements[index].updateTime = formatDate(new Date())
    return [200, success(agreements[index])]
  })

  // 删除协议
  mock.onDelete(new RegExp('/api/agreement/delete/\\d+')).reply(config => {
    const id = config.url.split('/').pop()
    const index = agreements.findIndex(item => item.id === id)
    if (index === -1) return [404, fail('协议不存在')]
    
    agreements.splice(index, 1)
    return [200, success(null)]
  })
} 